#!/usr/bin/env python
#_*_encoding:utf-8_*_ 
import logging
import time,os
from xml.dom.minidom import Document

logging.basicConfig(level=logging.DEBUG,
                format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                datefmt='%a, %d %b %Y %H:%M:%S',
                filename='myapp.log',
                filemode='w')

MACRO_DEBUG = False

#key value
enmKeyValue_None        =   0x00
enmKeyValue_SPACE       =   0x01
enmKeyValue_UP          =   0x02
enmKeyValue_DOWN        =   0x03
enmKeyValue_LEFT        =   0x04
enmKeyValue_RIGHT       =   0x05
enmKeyValue_LeftUp      =   0x06
enmKeyValue_LeftDown    =   0x07
enmKeyValue_RightUp     =   0x08
enmKeyValue_RightDown   =   0x09
enmKeyValue_Any         =   0x0A

enmKeyValue_LNormal     =   0x00
enmKeyValue_LPress      =   0x10
enmKeyValue_LTriggle    =   0x20
enmKeyValue_Balloon4    =   0x30
enmKeyValue_Balloon6    =   0x40
enmKeyValue_Balloon8    =   0x50
enmKeyValue_Balloon10    =   0x60
enmKeyValue_Balloon12    =   0x70
enmKeyValue_LKeyMask    =   0xF0

#for jz
RULE_SPACE  = 0x00
RULE_A      = 0x01
RULE_AA     = 0x02
RULE_AB     = 0x03
RULE_AAA     = 0x04
RULE_AAB     = 0x05
RULE_ABA     = 0x06
RULE_ABC     = 0x07

TGKeyNum    =   8   #TG每rule的最大按键数
YLKeyNum    =   4   #YL每rule的最大按键数

#玩法
enmGameMode_TG  =   0x01
enmGameMode_YL  =   0x02
enmGameMode_JZ  =   0x03

#将串s对齐到size位
def Align(s, size):
    r = ''
    if size == 32:
        if len(s) == 32:
            r = s
        elif len(s) == 16:
            for i in xrange(8):
                r += s[i*2:(i+1)*2] + '00'
        elif len(s) == 8:
            for i in xrange(4):
                r += s[i*2:(i+1)*2] + '00'*3
        elif len(s) == 4:
            for i in xrange(2):
                r += s[i*2:(i+1)*2] + '00'*7
        elif len(s) == 2:
            r += s + '00'*15
        return r
    
    elif size == 64:
        if len(s)==2:
            r = s+'00'*31
        if len(s)==4:
            r = s[0:2]+'00'*15+s[2:]+'00'*15
        if len(s)==8:
            for i in xrange(4):
                r += s[i*2:(i+1)*2]+'00'*7
        if len(s)==16:
            for i in xrange(8):
                r += s[i*2:(i+1)*2]+'00'*3
        if len(s)==32:
            for i in xrange(16):
                r += s[i*2:(i+1)*2]+'00'
        if len(s)==64:
            r = s
        return r
    return s


#对齐音效
def AlignVoice(s, size):
    #先检查是否有气球键
    flag = False
    r = ''
    if 'B' in s:
        #说明这串中有气球键
        flag = True
        r = s
    else:
        r = Align(s, size)
        
    return (flag, r)
    
#专门对齐气球键
def AlignBalloon(s, size):
    if size == 64:
        l = len(s)
        r = ''
        if l == 32:
            for i in xrange(l/2):
                ele = s[i*2:(i+1)*2]
                r += ele*2
        return r
    else:
        return s

#int格式化为需要的16进制
def int2hex(value):
    #l = len(hex(value))
    #return '0x'+(10-l)*'0'+hex(value)[2:]
    if value == 0:
        return ''
    s = hex(value)
    if s[-1] == 'L':
        s = s[:-1]
    if s == '0x':
        return ''
    if len(s) <= 10:
        #说明没有L
        if len(s)%2==1:
            return '0x'+'0'+s[2:]
        else:
            return s
    if len(s) > 10:
        if len(s)%2==1:
            return '0x'+'0'+s[2:]
        else:
            return s
    return ''

#根据串和键，生成按键
def GetKey(s,key):
    value = 0
    l = len(s)/2
    for i in xrange(l):
        element = s[2*i:(i+1)*2]
        if element=='00':
            continue
        if element=='01':
            v = key << i*8
            value |= v
    return value

#生成每一个rule,num表示取几位，它与每rule的最大按键数*2，因为2位表示一个按键
def AsignKeyByPos(key,body,thisround,pos, num, gamemode):
    if gamemode == enmGameMode_TG:
        n = 64
    elif gamemode == enmGameMode_YL:
        n = 32
    else:
        n = 64

    if pos==0:
        #r1~r4
        s = Align(body,n)
        for i in xrange(4):
            thisround.rule[i] |= GetKey(s[i*num:(i+1)*num],key)
    elif pos==1:
        if (gamemode == enmGameMode_YL and thisround.ft == '2') or gamemode==enmGameMode_TG:
            #YL只有ft=2时才可以进来
            #r4~r8
            s = Align(body,n)
            for i in xrange(4):
                thisround.rule[i+4] |= GetKey(s[i*num:(i+1)*num],key)
    return

#将s串变为01串，01表示该位置有键，什么键是不知道的            
def Convert(s):
    head = s.split(':')[0]
    body = s.split(':')[1]
    #print body
    r = ''
    if 'B' not in body:
        #串中没有气球键
        for i in xrange(len(body)/2):
            e = body[i*2:(i+1)*2]
            if e != '00':
                e = '01'
            else:
                e = '00'
            r += e
    else:
        #如果该串中有气球键
        for i in xrange(len(body)/2):
            e = body[i*2:(i+1)*2]
            if e != '00':
                e = '01'
            r += e*2
    return head+':'+r
    
    

#输出倒序的s(2位一个元素)
def Revert(s):
    '''
    Input: 0102030405060708
    Output:0807060504030201
    '''
    r = ''
    l = len(s)/2
    for i in xrange(l):
        r = s[i*2:(i+1)*2]+r
    return r

if __name__ == '__main__':
    pass
    